package com.eight.controller;

import com.eight.vo.ComResult;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO
 * @Author: Van.Planifolia
 * @CreateTime: 2023-03-08  20:35
 * @Version: 1.0
 */
@RestController
@RequestMapping("/rabbit/charge")
public class ChargeRabbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redis;

    /**
     * 投递一个消息，该消息是用来记录订单状态的
     *
     * @param id 订单的id
     * @return 生产的结果
     */
    @GetMapping("/makeChargeOrder/{id}")
    public ComResult makeChargeOrder(@PathVariable String id) {
        // 给普通交换机发送一个消息
        rabbitTemplate.convertAndSend("SE", "charge.in", "充值订单:" + id
                , message -> {
                    // 设置uuid
                    message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                    // 设置超时时间30秒
                    message.getMessageProperties().setDelay(5 * 1000 * 60);
                    // 设置消息id
                    message.getMessageProperties().setCorrelationId(id);
                    return message;
                }, new CorrelationData(id));

        return new ComResult(2000, "投递成功");
    }


}
